/**
 * 
 */
package com.jzweb.springbootmail.entity;

import java.io.IOException;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import com.jzweb.springbootmail.util.MailUtils;

/**
 * @author 030998
 *
 */
public class MailBox {

	private String mailId;//邮件ID
	private String mailType;//邮件类型，1收件箱、2发件箱
	private String subject;//主题
	private String from;
	private String to;
	private String cc;//抄送
	private String sc;//密送
	private String sendDate;//发送时间
	private boolean readFlag; //是否已读
	private String priority;//优先级1(High):紧急  3:普通(Normal)  5:低(Low) 
	private boolean replySign;//是否回执
	private int mailSize;//邮件大小
	private boolean attachFlag = false;//是否有附件
	private String folderId;//文件夹ID
	
	private String content;//邮件内容

	public String getMailId() {
		return mailId;
	}

	public void setMailId(String mailId) {
		this.mailId = mailId;
	}

	public String getMailType() {
		return mailType;
	}

	public void setMailType(String mailType) {
		this.mailType = mailType;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getCc() {
		return cc;
	}

	public void setCc(String cc) {
		this.cc = cc;
	}

	public String getSc() {
		return sc;
	}

	public void setSc(String sc) {
		this.sc = sc;
	}

	public String getSendDate() {
		return sendDate;
	}

	public void setSendDate(String sendDate) {
		this.sendDate = sendDate;
	}

	public boolean isReadFlag() {
		return readFlag;
	}

	public void setReadFlag(boolean readFlag) {
		this.readFlag = readFlag;
	}

	public String getPriority() {
		return priority;
	}

	public void setPriority(String priority) {
		this.priority = priority;
	}

	public boolean isReplySign() {
		return replySign;
	}

	public void setReplySign(boolean replySign) {
		this.replySign = replySign;
	}

	public int getMailSize() {
		return mailSize;
	}

	public void setMailSize(int mailSize) {
		this.mailSize = mailSize;
	}

	public boolean isAttachFlag() {
		return attachFlag;
	}

	public void setAttachFlag(boolean attachFlag) {
		this.attachFlag = attachFlag;
	}

	public String getFolderId() {
		return folderId;
	}

	public void setFolderId(String folderId) {
		this.folderId = folderId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Override
	public String toString() {
		return "MailBox [mailType=" + mailType + ", subject=" + subject + ", from=" + from + ", to=" + to + ", cc=" + cc
				+ ", sc=" + sc + ", sendDate=" + sendDate + ", readFlag=" + readFlag + ", priority=" + priority
				+ ", replySign=" + replySign + ", mailSize=" + mailSize + ", attachFlag=" + attachFlag + ", folderId="
				+ folderId + ", content=" + content + "]";
	}
	
	
	public void mail(String mailId,MimeMessage message) throws MessagingException, IOException {
		this.mailId = mailId;//邮件ID
		this.subject = MailUtils.getSubject(message);//邮件主题
		this.from = MailUtils.getFrom(message);
		this.to = MailUtils.getReceiveAddress(message, null);
		this.sendDate = MailUtils.getSentDate(message, null);//发送时间
		this.readFlag = MailUtils.isSeen(message);//是否已读
		this.priority = getPriority(message);//优先级
		this.replySign = MailUtils.isReplySign(message);//回执
		this.mailSize = message.getSize();//MB
		this.attachFlag = MailUtils.isContainAttachment(message);//是否有附件
		
		StringBuilder content = new StringBuilder();  
		MailUtils.getMailTextContent(message, content);
		this.content = content.toString();
        
	}
	
	/** 
     * 获得邮件的优先级 
     * @param msg 邮件内容 
     * @return 1(High):紧急  3:普通(Normal)  5:低(Low) 
     * @throws MessagingException  
     */  
    public static String getPriority(MimeMessage msg) throws MessagingException {  
        String priority = "3";  
        String[] headers = msg.getHeader("X-Priority");  
        if (headers != null) {  
            String headerPriority = headers[0];  
            if (headerPriority.indexOf("1") != -1 || headerPriority.indexOf("High") != -1)  
                priority = "1";  
            else if (headerPriority.indexOf("5") != -1 || headerPriority.indexOf("Low") != -1)  
                priority = "5";  
            else  
                priority = "3";  
        }  
        return priority;  
    }
	
}
